package com.bruce.downloader.thread;

import com.bruce.downloader.dto.MultipleDownInfoDto;

import java.io.File;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * 多线程下载线程
 * <br>CreateDate  2021/09/16 11:05:05
 *
 * @author luotuan
 * @version 1.0.0
 */
public class MultipleDownThread implements Callable<File> {
    private MultipleDownInfoDto infoDto;
    private CountDownLatch countDownLatch;

    public MultipleDownThread(MultipleDownInfoDto infoDto, CountDownLatch countDownLatch) {
        this.infoDto = infoDto;
        this.countDownLatch = countDownLatch;
    }

    public MultipleDownThread(MultipleDownInfoDto infoDto) {
        this.infoDto = infoDto;
    }

    @Override
    public File call() throws Exception {
        File file = infoDto.execute();
        if (Objects.nonNull(countDownLatch)) {
            countDownLatch.countDown();
        }
        return file;
    }

}
